﻿@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>加群管理</title>
    <script src="~/Scripts/EasyUI/jquery.min.js"></script>
    <script src="~/Scripts/EasyUI/jquery.easyui.min.js"></script>
    <link href="~/Scripts/EasyUI/themes/black/easyui.css" rel="stylesheet" />
    <link href="~/Scripts/EasyUI/themes/icon.css" rel="stylesheet" />
</head>
<body>


    <div>
        <div id="win1" style="width: 350px; height: 200px" class="easyui-window" data-options="modal:true,closed:true" title="添加群号">
            <table>
                <tr>
                    <td>类型：</td>
                    <td>
                        <select class="easyui-combobox" id="selTypes" style="width: 100px">
                        </select></td>
                    <tr>
                        <td>名称：</td>
                        <td>
                            <input type="text" class="textbox" id="add_txtName" /></td>
                    </tr>
                <tr>
                    <td>群号：</td>
                    <td>
                        <input type="text" class="textbox" id="add_txtCode" />
                    </td>
                </tr>
                <tr>
                    <td>群Key：</td>
                    <td>
                        <input type="text" class="textbox" id="add_txtKey" style="width: 250px" />
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <a href="javascript:void(0)" class="easyui-linkbutton" id="btnAddQun">确认添加</a>
                    </td>
                </tr>
            </table>

        </div>

        <div id="win2" style="width: 300px; height: 100px" class="easyui-window" data-options="modal:true,closed:true" title="添加类型">
            类型名称：<input type="text" id="txtTypeName" />
            <br/>
            端口名称：<input type="text" id="txtTypePortName" />
            <br/>
            <a href="javascript:void(0)" class="easyui-linkbutton" id="btnAddType">确认添加</a>
        </div>
        <a href="javascript:void(0)" class="easyui-linkbutton" id="btnopenAddQunHaoWin">添加群号</a>

        <div class="easyui-panel" title="群号管理">
            根据类型查询：<select id="selTypeForSearch" style="width: 150px">
            </select>

            根据部门查询：<select id="selName" style="width: 100px">
            </select>

            <table id="tableQun">
            </table>
        </div>
        <br />
        <a href="javascript:void(0)" class="easyui-linkbutton" id="btnopenAddTypeWin">添加类型</a>
        <br />
        <div class="easyui-panel" title="类型管理">
            <table id="typesTable"></table>
        </div>
            <div><a href="@Url.Action("LoginOut","JiaQQNew")">退出登录</a></div>
    </div>
</body>
</html>
<script type="text/javascript">

    var isload1 = false;
    var isload2 = false;
    $(function () {
        initEvent();
        getTypes();
        drawName();
    });

    function loadQunData() {
        if (isload1 && isload2) {
            var selType = $("#selTypeForSearch").combo("getValue");
            var selname = $("#selName").combo("getValue");
            $("#tableQun").datagrid({
                singleSelect: true,
                url: '@Url.Action("GetAllQunData", "JiaQQNew")?typeId=' + selType + "&Name=" + escape(selname),
                idField: 'Id',
                columns: [[
                          { field: 'Id', title: '标识', width: 100 },
                          { field: 'QQName', title: '部门', width: 100 },
                           { field: 'TypeName', title: '类型', width: 150 },
                           { field: 'QQKey', title: 'Key', width: 250 },
                          { field: 'QQCode', title: '群号', width: 100 },
                           { field: 'QQTypeId', title: 'TypeId', width: 100, hidden: true },
                           {
                               field: 'CreateTime', title: '添加时间', width: 150, formatter: function (value, row) {
                                   return value.replace('T', ' ');
                               }
                           },
                          {
                              field: 'statu', title: '操作', width: 200, formatter: function (value, row) {
                                  if (value == 1) {
                                      return "启用中"
                                  } else {
                                      return "<input type='button' value='启用' onclick='Qiyong(" + row.Id + "," + row.QQTypeId + ",\"" + row.QQKey + "\")' />&nbsp;<input type='button' value='删除' onclick='deleteQun(" + row.Id + ")'/>";
                                  }
                              }
                          }
                ]]

            });
        }
    }

    function deleteQun(id) {
        if (confirm("确认要删除吗？")) {
            $.ajax({
                url: '@Url.Action("DeleteQun", "JiaQQNew")',
                type: 'post',
                data: { Id: id },
                success: function (data) {
                    if (data == 'notlogin') {
                        alert("登录时间过期，请刷新页面重试！");
                    }
                    if (data == "1") {
                        alert("删除成功！");
                        loadQunData();
                    } else {
                        alert("删除失败");
                    }
                }
            });
        }
    }

    function drawName() {
        $('#selName').combobox({
            url: '@Url.Action("GetAllName", "JiaQQNew")',
            valueField: 'QQName',
            textField: 'QQName',
            onLoadSuccess: function () {
                var data = $('#selName').combobox('getData');
                isload1 = true;
                if (data.length > 0) {
                    $("#selName").combobox('select', data[0].QQName);
                }
             
            },
            onSelect: function () {
                loadQunData();
            }
        });
    }


    function drawTypes(types) {
        $('#selTypes').combobox({
            data: types,
            valueField: 'Id',
            textField: 'TypeName',
            onLoadSuccess: function () {
                var data = $('#selTypes').combobox('getData');
                if (data.length > 0) {
                    $("#selTypes").combobox('select', data[0].Id);
                }
            }
        });

        var tempTypes = [];
        tempTypes.push({ Id: "-1", TypeName: "全部" });
        for (var i = 0; i < types.length; i++) {
            tempTypes.push(types[i]);
        }
        $('#selTypeForSearch').combobox({
            data: tempTypes,
            valueField: 'Id',
            textField: 'TypeName'
            ,
            onLoadSuccess: function () {
                var data = $('#selTypeForSearch').combobox('getData');
                isload2 = true;
                if (data.length > 0) {
                    $("#selTypeForSearch").combobox('select', data[0].Id);
                }
            }
            ,
            onSelect: function () {
                loadQunData();
            }
        });



        $('#selTypeForSearch').combobox()
        $("#typesTable").datagrid({
            data: types,
            idField: 'Id',
            singleSelect: true,
            columns: [[
                      { field: 'Id', title: '标识', width: 100 },
                      { field: 'TypeName', title: '名称', width: 150 },
                       { field: 'typePortName', title: '端口名', width: 150 },
                      {
                          field: 'lujing', title: '加群路径', width: 400,
                          formatter: function (value, row) {
                              return "@System.Configuration.ConfigurationManager.AppSettings["toJiaQun"]?typeId=" + row.Id;
                          }
                      },
                      {
                          field: 'caozuo', title: '操作', width: 100,
                          formatter: function (value, row) {
                              return "<input type='button' value='删除' onclick='deleteType(" + row.Id + ")'>"
                          }
                      }
            ]]

        });
                  }


                  function getTypes() {
                      $.ajax({
                          url: '@Url.Action("GetAllTypes", "JiaQQNew")',
                          type: 'post',
                          data: {},
                          dataType: 'json',
                          success: function (data) {
                              drawTypes(data);
                          }
                      });
                  }


                  function Qiyong(id, typeId, key) {
                      if (confirm("确定要启用吗")) {
                          $.ajax({
                              url: '@Url.Action("QiYong", "JiaQQNew")',
                              type: 'post',
                              data: { Id: id, typeId: typeId, key: key },
                              success: function (data) {
                                  if (data == "1") {
                                      loadQunData();
                                  }
                              }
                          });
                      }
                  }

                  function deleteType(id) {
                      if (confirm("确定要删除吗？")) {
                          $.ajax({
                              url: '@Url.Action("DeleteType", "JiaQQNew")',
                              type: 'post',
                              data: { Id: id },
                              success: function (data) {
                                  if (data == 'notlogin') {
                                      alert("登录时间过期，请刷新页面重试！");
                                  }
                                  if (data == "1") {
                                      alert("删除成功！");
                                      getTypes();
                                  } else {
                                      alert("删除失败");
                                  }
                              }
                          });
                      }
                  }

                  function initEvent() {
                      $("#btnopenAddQunHaoWin").click(function () {
                          $('#win1').window('open');
                      });

                      $("#btnopenAddTypeWin").click(function () {
                          $('#win2').window('open');
                      });

                      $("#btnAddType").click(function () {
                          var typeName = $("#txtTypeName").val();
                          var typePortName=$("#txtTypePortName").val();
                          if ($.trim(typeName) != "") {
                              $.ajax({
                                  url: '@Url.Action("DoAddType", "JiaQQNew")',
                                  type: 'post',
                                  data: { typeName: typeName, typePortName: typePortName },
                                  success: function (data) {
                                      if (data == 'notlogin') {
                                          alert("登录时间过期，请刷新页面重试！");
                                      } else {
                                          if (data == "1") {
                                              alert("添加成功！");
                                              $('#win2').window('close');
                                              getTypes();
                                          } else {
                                              alert("添加失败！");
                                          }
                                      }
                                  }
                              });
                          } else {
                              alert("类型名称不能为空!");
                          }
                      });

                      $("#btnAddQun").click(function () {
                          var selType = $("#selTypes").combo("getValue");
                          var add_txtName = $.trim($("#add_txtName").val());
                          var add_txtCode = $.trim($("#add_txtCode").val());
                          var add_txtKey = $.trim($("#add_txtKey").val());
                          if (add_txtName != "" && add_txtCode != "" && add_txtKey != "") {
                              $.ajax({
                                  url: '@Url.Action("DoAddQunData", "JiaQQNew")',
                                  data: { txtName: add_txtName, number: add_txtCode, key: add_txtKey, typeId: selType },
                                  type: 'post',
                                  success: function (data) {
                                      if (data == 'notlogin') {
                                          alert("登录时间过期，请刷新页面重试！");
                                      }
                                      else {
                                          if (data == "1") {
                                              alert("添加成功！");
                                              $('#win1').window('close');
                                              drawName();
                                          } else {
                                              alert(data);
                                          }
                                      }
                                  }
                              });
                          } else {
                              alert("数据都不能为空");
                          }


                      });
                  }
</script>
